<?php

include('/var/www/html/new/d/common/autoload.php');
// include_once("/var/www/html/new/conf/yd.le.je");
// include_once("/var/www/html/new/d/common/yd.le.je");

// $ydle=new yd_go_sql();
// $ydle->connect(); 
use \pft\Prod\Product;
use \pft\Member\MemberAccount;
use pft\Prod\Ticket;
$ptypeList = array('A','B','C','D','F','G');//
$action = $_REQUEST['action'];
$applyid = $_SESSION['memberID'];
$prod = new Product($GLOBALS['le']);
$mem = new MemberAccount($GLOBALS['le']);
$soap = SoapInit::GetSoapInside();

$applydid = $_SESSION['memberID'];

switch($action) {
    // 检测订单支付状态
    case 'CheckOrderPay':
        include("/var/www/html/new/d/class/OrderRecord.php");
        $ordernum=$_REQUEST['orderid'];
        $orders = array();
        if(substr($ordernum,0,2)=='st'){ // 是否合并的订单
            $sql="SELECT old FROM uu_unitpay WHERE new='$ordernum' AND status=1 LIMIT 1";
            $GLOBALS['le']->query($sql);
            $GLOBALS['le']->fetch_assoc();
            $subOrders = $le->f('old');
            $subOrders = explode(',', $subOrders);
            foreach($subOrders as $v){
                $or=new OrderRecord($v,$soap);
                $order=$or->info();
                if($order['mainOrder']->UUpmode==1 && $order['mainOrder']->UUpaystatus!=1){
                   exit('0');
                }
            }
        }else{
            $or=new OrderRecord($ordernum,$soap);
            $order=$or->info();
            if($order['mainOrder']->UUpmode==1 && $order['mainOrder']->UUpaystatus!=1){
               exit('0');
            }
        }

        exit('1');
        break;
    case 'NoticeList':
        if (!$_SESSION['domain_mid']) {
            echo '[]';
            break;
        }
        if ($_GET['test']) {
            echo $_SESSION['domain_mid'];
        }
        $sql = <<<SQL
SELECT id,title,btime FROM uu_notice
WHERE adminid={$_SESSION['domain_mid']} AND status<2 ORDER BY btime DESC
 LIMIT 50
SQL;
        $GLOBALS['le']->query($sql);
        $out = array();
        while($row=$GLOBALS['le']->fetch_assoc()) {
            $out[] = $row;
        }
        echo json_encode($out);
        break;
    case 'NoticeDetail':
        $id = intval($_GET['id']);
        $sql = "SELECT title,btime,content FROM uu_notice WHERE id=$id LIMIT 1";
        $GLOBALS['le']->query($sql);
        $row = $GLOBALS['le']->fetch_assoc();
        $row['content'] = htmlspecialchars_decode(nl2br($row['content']));
        echo json_encode($row);
        break;
    case 'RealTimeOrder':
        $out = array();
        if (isset($_SESSION['domain_mid'])) {
            $order_xml = $soap->Order_Globle_Search(
                "", "{$_SESSION['domain_mid']}", "", "", "", "", "", "", "", "",
                "", "",$ordernum, "", "", "", "", "", "", "1",
                "0", "10", "0", "0","", "0", "", "0", "", "",
                "", "1", "0","0","","");
            $order_arr = simplexml_load_string($order_xml);
            foreach ($order_arr as $order) {
                $out[] = array(
                    'ordertime' => (string)$order->UUordertime,
                    'tnum'      => (int)$order->UUtnum,
                    'ordername' => utf8_substr_replace((string)$order->UUordername, '*', 1,1) ,
                    'ltitle'    => (string)$order->UUltitle,
                    'ttitle'    => (string)$order->UUttitle,
                    'lid'       => (int)$order->UUlid,
                );
            }
        } else {
            $out = array(array());
        }
        echo json_encode($out);
        break;
        
    case 'validateCode':
        if(trim(strtolower($_POST['pcode']))!=$_SESSION['validateCode'] || $_POST['mcode']!=$_SESSION['vcode'])
            exit('{"status":"fail","msg":"图形验证码或手机验证码错误"}');        
        $_SESSION['mobile'] = $_SESSION['mobile_s'] ;
        echo '{"status":"ok","msg":"恭喜您验证通过喇"}';
        break;
    case 'mobileValidateCode':
        if(!ismobile($_POST['mobile']))
            exit('{"code":"102","msg":"请输入正确的手机号码"}');
        $sms_file = '/var/www/html/new/d/tpl/sms/register.txt';
        if(!file_exists($sms_file)) 
            exit('{"code":"102","msg":"短信模版不存在"}');
        $tpl = file_get_contents($sms_file);
        $time_diff = $_SERVER['REQUEST_TIME']-$_SESSION['timer'];
        if(!isset($_SESSION['timer']) || $time_diff>5) {
            // $r = MemberAccount::sendVcode($_POST['mobile'], $tpl, $soap, true);
             // if($r==100) {      
                //假的验证码数据
                $_SESSION['vcode'] = genRandomString(6);
                $_SESSION['timer'] = $_SERVER['REQUEST_TIME'];                
                
                session_start();
                $_SESSION['mobile_s'] = $_POST['mobile'];
                echo '{"status":"100","msg":"发送验证码成功！" '.$mail_msg.',"passport":"'.$mobile.'"}';
            // }
            // else {
                // echo '{"status":"102","msg":"对不起，短信服务器发生故障，造成的不便我们感到十分抱歉。请联系我们客服人员","code":'.$r.'}';
            // }
        }
        else {
            echo '{"status":"102","msg":"发送间隔太短！请在60秒后再重试"}';
        }
        break;
    case 'order_cancel':
        include '/var/www/html/new/d/class/OrderRecord.php';
        include "/var/www/html/new/d/class/AccessRecord.php";
        $ordernum=$_REQUEST['ordernum'];
        $or=new OrderRecord($ordernum,$soap);
        $orders=$or->info();
        $concat = $_REQUEST['concat'] ? $_REQUEST['concat'] : 0;
        // 记录访问信息
		if(AccessRecord::insert(1,"退款|$ordernum|{$_SESSION['mobile']}")){
			$result = array();
			try{
				$result = $or->cancel(0,$concat);
                print_r($result);exit;
			} catch(Exception $e) {
				$out=array("outcome"=>-1,"msg"=>$e->getMessage());
				exit(json_encode($out));
			}
			if($result['code']==100){
				$msg="您".$orders['mainOrder']->UUltitle.$orders['mainOrder']->UUttitle."的订单".(string)$orders['mainOrder']->UUordernum."已被取消。【票付通】";
				$s->Send_SMS_System_Fee((string)$orders['mainOrder']->UUordertel,$msg,(string)$orders['mainOrder']->UUaid,(string)$orders['mainOrder']->UUordernum);
				$out=array("outcome"=>1);				
			}else{ 
				$out=array("outcome"=>-1,"msg"=>$errCode[$result['msg']]);
			}
		}else{ 
			$out=array("outcome"=>-1,"msg"=>'操作信息记录失败');
		}
        
		echo json_encode($out);
        break;
    default:
        break;
}
function utf8_substr_replace($original, $replacement, $position, $length)
{
    $startString = mb_substr($original, 0, $position, "UTF-8");
    $endString = mb_substr($original, $position + $length, mb_strlen($original), "UTF-8");

    $out = $startString . $replacement . $endString;

    return $out;
}

